package com.example.wang.ghtoonui.homepage;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.blankj.utilcode.utils.ScreenUtils;
import com.example.wang.ghtoonui.R;
import com.orhanobut.logger.Logger;
import com.youth.banner.Banner;

import java.util.Arrays;
import java.util.List;

/**
 * Created by wang on 2017/3/20.
 */
public class GHHomepageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private int screenWidth = 0;

    //type 横幅 快捷功能 公告 加入和帮助 工会之窗1 工会之窗2
    public static final int TYPE_BANNER = 0xff01;
    public static final int TYPE_QUICK_FUNCTION = 0xff02;
//    public static final int TYPE_ANNOUNCEMENT = 0xff03;
//    public static final int TYPE_MEMBERSHIP_AND_HELP = 0xff04;
//    public static final int TYPE_GH_WINDOW_ONE = 0xff05;
//    public static final int TYPE_GH_WINDOW_TWO = 0xff06;

    public GHHomepageAdapter(Context context) {
        this.context = context;
        screenWidth = ScreenUtils.getScreenWidth(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Logger.d("onCreateViewHolder parent is " + parent + ",viewType is " + viewType);
        switch (viewType) {
            case TYPE_BANNER:
                return new HolderBanner(LayoutInflater.from(parent.getContext()).inflate(R.layout.gh_homepage_banner_item,parent,false));
            case TYPE_QUICK_FUNCTION:
                LinearLayout p = (LinearLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.gh_homepage_quick_function_recycle, parent, false);
                return new HolderQuickFunction(p);
            default:
                Log.d("error", "viewholder is null");
//                return new HolderBanner(LayoutInflater.from(parent.getContext()).inflate(R.layout.gh_homepage_banner_item,parent,false));
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Logger.d("onBindViewHolder position is " + position + ",holder is " + holder);
        if (holder instanceof HolderBanner){
            bindTypeBanner((HolderBanner)holder,position);
        }else if (holder instanceof HolderQuickFunction){
            bindTypeQuickFunction((HolderQuickFunction)holder,position);
        }
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
//        Logger.d("getItemViewType position is " + position);
        if (position == 0){
            return TYPE_BANNER;
        }else if (position==1){
            return TYPE_QUICK_FUNCTION;
        }
//        else if (position == 2){
//            return TYPE_ANNOUNCEMENT;
//        }else if (position == 3){
//            return TYPE_MEMBERSHIP_AND_HELP;
//        }else if (position == 4){
//            return TYPE_GH_WINDOW_ONE;
//        }else if (position == 5){
//            return TYPE_GH_WINDOW_TWO;
//        }
        else {
            //TODO 默认返回横幅界面，预留设计为一个错误界面
            return TYPE_BANNER;
        }

    }

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type){
                        case TYPE_BANNER:
                            return gridManager.getSpanCount();
                        case TYPE_QUICK_FUNCTION:
                            return gridManager.getSpanCount();
                        default:
                            return 3;
                    }
                }
            });
        }
    }

    private void bindTypeBanner(HolderBanner holder, int position) {
        String [] urls = this.context.getResources().getStringArray(R.array.urls);
        List<String> bannerUrlList = Arrays.asList(urls);
        holder.banner.setImages(bannerUrlList)
                .setImageLoader(new FrescoImageLoader())
                .start();
//        banner.setOnBannerListener(this);
    }

    private void bindTypeQuickFunction(HolderQuickFunction holder, int position){
        holder.quickFunctionRecycler.setLayoutManager(new GridLayoutManager(context, 6));
        FunctionAdapter adapter = new FunctionAdapter(context,R.layout.gh_homepage_quick_function_item,DataGenerator.getQuickFunctions());
        holder.quickFunctionRecycler.setAdapter(adapter);
    }

    public class HolderBanner extends RecyclerView.ViewHolder{
        public Banner banner;

        public HolderBanner(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);
        }
    }

    public class HolderQuickFunction extends RecyclerView.ViewHolder{
        public RecyclerView quickFunctionRecycler;

        public HolderQuickFunction(View itemView) {
            super(itemView);
            quickFunctionRecycler = (RecyclerView)itemView.findViewById(R.id.quick_function_recycle);
        }
    }
}
